package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat01/basic/config"
	"wechat01/handler/model"
	"wechat01/handler/request"
)

func AddPost(c *gin.Context) {
	var form request.AddPost
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	userId := c.GetFloat64("userId")
	post := model.Post{
		UserId:    int(userId),
		Content:   form.Content,
		PostImage: form.PostImage,
	}
	if err := post.AddPost(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "发布朋友圈失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布朋友圈成功",
	})
	return
}
func GetPost(c *gin.Context) {
	userId := c.GetFloat64("userId")
	var post model.Post
	list, err := post.GetPost(config.DB, int(userId))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询朋友圈列表失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询朋友圈列表成功",
		"data": gin.H{
			"list": list,
		},
	})
	return
}
